class websocketUtil{

     websocket;
     _callback;
     constructor(url,callback){
        this._callback = callback
        this.createSocket(url);
     }

     createSocket(url){
        var that = this;
        this.websocket = wx.connectSocket({
          url: url,
          success:function(e){
              console.log("websoket connect success") 
          },
        })
        this.websocket.onClose(function(e){
          this.websocket = null;
          console.log('socket closed')
          setTimeout(function () {
            console.log('reconncting ...')
            that.createSocket(url);
         }, 3000)
   
        })

        this.websocket.onError(function(e){
          this.websocket = null;
          console.log('socket error closed')
          setTimeout(function () {
            console.log('reconncting ...')
            //that.createSocket(url);
         }, 3000)
        })

        this.websocket.onMessage(function(e){
          that._callback(e)
        })
     }



     send(msg){
      this.websocket.send({
        data:msg,
        success:function(e){
          console.log("send success")
        }
      })
     }


}
export default  websocketUtil;